﻿/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.Collections;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using PowerShell.Host.PsTFS.CdmLet.PsObject;

namespace PowerShell.Host.PsTFS.CdmLet
{
    /// <summary>
    /// Récupération de la liste des projets d'un serveur TFS
    /// </summary>
    [Cmdlet(VerbsCommon.Get, "AllProjects")]
    public class Get_AllProjects : Cmdlet
    {
        public Get_AllProjects()
        {
            if (TFSProvider.s_TFSDriveInfo != null)
            {
                this.TFS = TFSProvider.s_TFSDriveInfo.TFS;
            }

        }
        protected override void ProcessRecord()
        {
            if (TFS == null)
            {
                WriteError(new ErrorRecord(new ArgumentException("No server TFS!!!"), "PsTFS", ErrorCategory.ObjectNotFound, TFS));
            }         
            List<Project> lp = new List<Project>();
            try
            {
                WorkItemStore witstore = PsService.WIS(this.TFS);

                // Parcour les projets du serveur
                foreach (Project p in witstore.Projects)
                {
                    // Ajout du projet à la liste
                    lp.Add(p);
                }

                // Renvois de la liste des projets
                WriteObject(lp);
            }
            catch (Exception ex)
            {
                ErrorRecord err = new ErrorRecord(ex, "<ID>", ErrorCategory.WriteError, null);
                WriteError(err);
                PsLog.AddLog(ex.StackTrace);
            }

        }
        /// <summary>
        /// Permet d'informer la fin de la commande
        /// </summary>
        protected override void EndProcessing()
        {
            PsLog.AddLog(null, "Get-AllProject is end");
            base.EndProcessing();
        }

        /// <summary>
        /// Object contenant Team Foundation Server 2008
        /// </summary>
        [Parameter(Position = 0)]
        public TeamFoundationServer TFS { get; set; }
    }
}